﻿using CC.Models;
using Dapper;
using MX.Core.ORM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CC.BLLs
{
    public class CommentBLL
    {
        static public long Insert(Comment comment)
        {
            using (var conn = DatabaseHelper.Get())
            {
                using (var tran = conn.BeginTransaction())
                { 
                    conn.Execute(@"INSERT INTO  `comment` ( 
                                            `ParentId`,
                                            `MemberId`,
                                            `OutId`,
                                            `Context`,
                                            `LikeCount`,
                                            `ReplyCount`,
                                            `CreateTime`,
                                            `UpdateTime`,
                                            `HotVal`,
                                            `FixTop`,
                                            `Type`
                                        ) 
                                        VALUES
                                            ( 
                                            @ParentId,
                                            @MemberId,
                                            @OutId,
                                            @Context,
                                            @LikeCount,
                                            @ReplyCount,
                                            @CreateTime,
                                            @UpdateTime,
                                            @HotVal,
                                            @FixTop,
                                            @Type
                                            )    ", comment, tran);
                    var commentId = conn.ExecuteScalar<long>("SELECT LAST_INSERT_ID()", transaction: tran);
                    tran.Commit();
                    return commentId;
                }

            }
        }

        static public PagerResult<Comment> List(string outId, long parentId, int pageSize, int pageIndex)
        {
            using (var conn = DatabaseHelper.Get())
            {
                string strSql = "select * from comment where OutId=@OutId  ORDER BY CommentId Desc";
                if (parentId > 0)
                    strSql = "select * from comment where ParentId=@ParentId ORDER BY CommentId Desc";
                return conn.PageList<Comment>(strSql, pageIndex, pageSize, new { OutId = outId, ParentId = parentId });
            }
        }

        static public List<Comment> ChildList(IList<long> parentIds)
        {
            if (parentIds.Count < 1)
            {
                return new List<Comment>();
            }
            using (var conn = DatabaseHelper.Get())
            {
                string sql = string.Join(";", parentIds.Select(id => $"select * from `comment` where ParentId={id} ORDER BY CommentId Desc LIMIT 0, 1").ToArray());
                using (var multi = conn.QueryMultiple(sql))
                {
                    return multi.Read<Comment>().ToList();
                }
            }
        }
    }
}
